package com.ruoyi.manage.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.manage.domain.SalSalary;
import com.ruoyi.manage.service.ISalSalaryService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 薪资Controller
 * 
 * @author cf
 * @date 2025-06-05
 */
@RestController
@RequestMapping("/manage/salary")
public class SalSalaryController extends BaseController
{
    @Autowired
    private ISalSalaryService salSalaryService;

    /**
     * 查询薪资列表
     */
    @PreAuthorize("@ss.hasPermi('manage:salary:list')")
    @GetMapping("/list")
    public TableDataInfo list(SalSalary salSalary)
    {
        startPage();
        List<SalSalary> list = salSalaryService.selectSalSalaryList(salSalary);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('manage:salary:list')")
    @GetMapping("/calculate")
    public TableDataInfo calculate(@RequestParam("staffId") Long staffId, @RequestParam("nowMonth") Date nowMonth)
    {
        SalSalary salary = new SalSalary();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        List<SalSalary>  salaries=salSalaryService.calculateSalary(staffId, sdf.format(nowMonth));

        return getDataTable(salaries);
    }

    /**
     * 导出薪资列表
     */
    @PreAuthorize("@ss.hasPermi('manage:salary:export')")
    @Log(title = "薪资", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SalSalary salSalary)
    {
        List<SalSalary> list = salSalaryService.selectSalSalaryList(salSalary);
        ExcelUtil<SalSalary> util = new ExcelUtil<SalSalary>(SalSalary.class);
        util.exportExcel(response, list, "薪资数据");
    }

    /**
     * 获取薪资详细信息
     */
    @PreAuthorize("@ss.hasPermi('manage:salary:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(salSalaryService.selectSalSalaryById(id));
    }

    /**
     * 新增薪资
     */
    @PreAuthorize("@ss.hasPermi('manage:salary:add')")
    @Log(title = "薪资", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SalSalary salSalary)
    {
        return toAjax(salSalaryService.insertSalSalary(salSalary));
    }

    /**
     * 修改薪资
     */
    @PreAuthorize("@ss.hasPermi('manage:salary:edit')")
    @Log(title = "薪资", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SalSalary salSalary)
    {
        return toAjax(salSalaryService.updateSalSalary(salSalary));
    }

    /**
     * 删除薪资
     */
    @PreAuthorize("@ss.hasPermi('manage:salary:remove')")
    @Log(title = "薪资", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(salSalaryService.deleteSalSalaryByIds(ids));
    }
}
